package com.khoayang.littlefighterchaos.game;

import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.w3c.dom.Node;

public class Bar {
	private CCSprite _SpriteBar;
	private CCSprite _SpriteFillBar;
	private float _FillPercent;
	private CGPoint _Position;
	
	public Bar(){
		_SpriteBar = null;
		_SpriteFillBar = null;
		_FillPercent = 100;
		_Position = CGPoint.ccp(0, 0);
	}
	
	public CCSprite get_SpriteBar() {
		return _SpriteBar;
	}
	public void set_SpriteBar(CCSprite _SpriteBar) {
		this._SpriteBar = _SpriteBar;
	}
	public CCSprite get_SpriteFillBar() {
		return _SpriteFillBar;
	}
	public void set_SpriteFillBar(CCSprite _SpriteFillBar) {
		this._SpriteFillBar = _SpriteFillBar;
	}
	public float get_FillPercent() {
		return _FillPercent;
	}
	public void set_FillPercent(float fillPercent) {
		this._FillPercent = fillPercent;
		set_Position(_Position);
	}
	
	public void loadBarFromXMLNode(Node node){
		for(int i=0; i<node.getChildNodes().getLength(); i++){
			Node nodeTemp = node.getChildNodes().item(i);
			String nodeName = nodeTemp.getNodeName();
			if(nodeName.compareTo("BarSprite") == 0){
				_SpriteBar = CCSprite.sprite(nodeTemp.getTextContent());
			}else if(nodeName.compareTo("BarFillSprite") == 0){
				_SpriteFillBar = CCSprite.sprite(nodeTemp.getTextContent());
			}
		}
	}
	
	public CGPoint get_Position() {
		return _Position;
	}
	
	public void set_Position(CGPoint position) {
		this._Position = position;
		if(position != null){
			_SpriteBar.setPosition(position.x, position.y);
			float dx = _SpriteFillBar.getContentSize().width;
			dx = dx -  dx * _FillPercent/100.0f;
			_SpriteFillBar.setScaleX(_FillPercent/100.0f);
			_SpriteFillBar.setPosition(_Position.x - dx/2, _Position.y);
		}
	}
	
	public Bar clone(){
		Bar bar = new Bar();
		bar.set_SpriteBar(CCSprite.sprite(_SpriteBar.getTexture()));
		bar.set_SpriteFillBar(CCSprite.sprite(_SpriteFillBar.getTexture()));
		if(_Position != null)
			bar.set_Position(CGPoint.ccp(_Position.x, _Position.y));
		bar.set_FillPercent(_FillPercent);
		return bar;
	}
}
